﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;

namespace QuanLyKho.QuanLyKho
{
    public partial class PhieuYeuCau : UserControl
    {
        public PhieuYeuCau()
        {
            InitializeComponent();
            LoadData();
            gcID.Visible = false;
            gcID1.Visible = false;
            gcTuNgay.Visible = false;
            gcDenNgay.Visible = false;
        }

        private void LoadData()
        {
            gridPhieuYeuCau.DataSource = DataAll.SelectAll("PhieuYeuCau inner join PhongMau_SanPham on PhieuYeuCau.IdSanPham = PhongMau_SanPham.IdSanPham",
                "PhieuYeuCau.[PhongBan],PhieuYeuCau.[ID],PhongMau_SanPham.[MaHang] as 'MaSanPham',PhongMau_SanPham.[TuNgay],PhongMau_SanPham.[DenNgay],PhieuYeuCau.[NgayYeuCau],PhieuYeuCau.[NguoiYeuCau],PhieuYeuCau.[SoLuong],PhieuYeuCau.[TrangThai],PhieuYeuCau.[NgayChungTu],PhieuYeuCau.[NgayHoachToan],PhieuYeuCau.[SoChungTu],PhieuYeuCau.[GhiChu],PhieuYeuCau.[PhongBan],PhieuYeuCau.[LoaiPhieu]",
                "WHERE PhieuYeuCau.[TrangThai]= " + Convert.ToInt32(TrangThai.PhieuYeuCau_ChoXuLy) + " AND LoaiPhieu = " + Convert.ToInt32(LoaiPhieuYeuCau.TraLai));
            gridDaXuLy.DataSource = DataAll.SelectAll("PhieuYeuCau inner join PhongMau_SanPham on PhieuYeuCau.IdSanPham = PhongMau_SanPham.IdSanPham",
                "PhieuYeuCau.[PhongBan],PhieuYeuCau.[ID],PhongMau_SanPham.[MaHang] as 'MaSanPham',PhieuYeuCau.[NgayYeuCau],PhieuYeuCau.[NguoiYeuCau],PhieuYeuCau.[SoLuong],PhieuYeuCau.[TrangThai],PhieuYeuCau.[NgayChungTu],PhieuYeuCau.[NgayHoachToan],PhieuYeuCau.[SoChungTu],PhieuYeuCau.[GhiChu],PhieuYeuCau.[PhongBan],PhieuYeuCau.[LoaiPhieu]",
                "WHERE PhieuYeuCau.[TrangThai]= " + Convert.ToInt32(TrangThai.PhieuYeuCau_CapNhatTraLai) + " AND LoaiPhieu = " + Convert.ToInt32(LoaiPhieuYeuCau.TraLai));
        }

        private void gvPhieuYeuCau_DoubleClick(object sender, EventArgs e)
        {
            if (gvPhieuYeuCau.FocusedRowHandle >= 0)
            {
                PhieuYeuCau_ChiTiet.MaID = gvPhieuYeuCau.Columns["ID"].View.GetRowCellDisplayText(gvPhieuYeuCau.FocusedRowHandle, "ID");
                var lss = new PhieuYeuCau_ChiTiet();
                lss.ShowDialog();
                LoadData();
                PhieuYeuCau_ChiTiet.MaID = "0";
            }
        }

        private void gvDaXuLy_DoubleClick(object sender, EventArgs e)
        {
            if (gvDaXuLy.FocusedRowHandle >= 0)
            {
                PhieuYeuCau_ChiTiet.MaID = gvDaXuLy.Columns["ID"].View.GetRowCellDisplayText(gvDaXuLy.FocusedRowHandle, "ID");
                var lss = new PhieuYeuCau_ChiTiet();
                lss.ShowDialog();
                LoadData();
                PhieuYeuCau_ChiTiet.MaID = "0";
            }
        }

        private void btnCapNhatKho_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvPhieuYeuCau.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn cập nhật kho ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Update("PhieuYeuCau", "TrangThai =" + Convert.ToInt32(TrangThai.PhieuYeuCau_CapNhatTraLai) + " WHERE ID =" + dataview[gvPhieuYeuCau.FocusedRowHandle]["ID"]);
                if (message.Equals("success"))
                {
                    DataAll.InsertAll("HanhTrinh", dataview[gvPhieuYeuCau.FocusedRowHandle]["MaSanPham"]
                        + "," + Convert.ToInt32(PhongBan.KhoSX)
                        + "," + dataview[gvPhieuYeuCau.FocusedRowHandle]["SoChungTu"]
                        + "," + Convert.ToInt32(TrangThai.PhieuYeuCau_CapNhatTraLai)
                        + "," + Main.UserID+ "," + DateTime.Now.ToString("MM/dd/yyyy")
                        + "," + dataview[gvPhieuYeuCau.FocusedRowHandle]["SoLuong"]
                        + "," + Convert.ToDateTime(dataview[gvPhieuYeuCau.FocusedRowHandle]["TuNgay"]).ToString("MM/dd/yyyy")
                        + "," + Convert.ToDateTime(dataview[gvPhieuYeuCau.FocusedRowHandle]["DenNgay"]).ToString("MM/dd/yyyy"));
                }
                // cập nhật kho
                DataTable dt = DataAll.SelectAll("PhieuYeuCau_ChiTiet", "*", "where IDPhieuYeuCau = " + dataview[gvPhieuYeuCau.FocusedRowHandle]["ID"]);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var dtVatTu = DataAll.SelectAll("Kho_VatTu", "*", " WHERE IDVatTu=" + dt.Rows[i]["IDVatTu"]);
                    var dtKhoAo = DataAll.SelectAll("Kho_VatTuDuKien", "*", " WHERE MaVatTu=N'" + dtVatTu.Rows[0]["MaVatTu"] + "'");
                    double soluong = Convert.ToDouble(dtVatTu.Rows[0]["SoLuong"]) + Convert.ToDouble(dt.Rows[i]["SoLuong"]);
                    double soluongKhoAo = Convert.ToDouble(dtKhoAo.Rows[0]["SoLuong"]) + Convert.ToDouble(dt.Rows[i]["SoLuong"]);
                    DataAll.Update("Kho_VatTu", "SoLuong = " + soluong + " WHERE IDVatTu=" + dt.Rows[i]["IDVatTu"]);
                    DataAll.Update("Kho_VatTuDuKien", "SoLuong = " + soluongKhoAo + " WHERE MaVatTu=N'" + dtVatTu.Rows[0]["MaVatTu"] + "'");    
                }

                MessageBox.Show(message.Equals("success") ? "Cập nhật kho thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                LoadData();
            }
        }

        private void gvDaXuLy_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
            if (e.Column.FieldName == "LoaiPhieu")
            {
                e.DisplayText = Common.HienThiLoaiPhieuYeuCau(Convert.ToInt32(e.Value));
            }
            if (e.Column.FieldName == "PhongBan")
            {
                e.DisplayText = Common.HienThiPhongBan(Convert.ToInt32(e.Value));
            }
        }

        private void gvPhieuYeuCau_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
            if (e.Column.FieldName == "LoaiPhieu")
            {
                e.DisplayText = Common.HienThiLoaiPhieuYeuCau(Convert.ToInt32(e.Value));
            }
            if (e.Column.FieldName == "PhongBan")
            {
                e.DisplayText = Common.HienThiPhongBan(Convert.ToInt32(e.Value));
            }
        }
    }
}
